package com.stone.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stone.bean.AppResponse;
import com.stone.bean.BasePage;
import com.stone.bean.dto.RoleAddDTO;
import com.stone.bean.dto.RoleMenuAddDTO;
import com.stone.bean.dto.RoleUpdateDTO;
import com.stone.constant.Const;
import com.stone.entity.SysRole;
import com.stone.entity.SysRoleMenu;
import com.stone.service.SysRoleMenuService;
import com.stone.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/backstage/sysRole")
@Api(tags = "系统角色管理")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @PostMapping("/insert")
    @ApiOperation("保存角色")
    public AppResponse addRole(@RequestBody @Valid RoleAddDTO dto, @RequestHeader(value = Const.AUTH) String token) {
        dto.setToken(token);
        sysRoleService.addRole(dto);
        return AppResponse.success();
    }

    @PostMapping("/query")
    @ApiOperation("查询角色")
    public AppResponse<List<SysRole>> queryRole(BasePage dto) {
        Page<SysRole> rolePage = new Page<>(dto.getPage(), dto.getPageSize());
        IPage<SysRole> page = sysRoleService.page(rolePage, new QueryWrapper<SysRole>().orderByDesc("id"));
        return AppResponse.success().setRecords(page.getRecords()).setTotal(page.getTotal());
    }

    @PostMapping("/update")
    @ApiOperation("修改角色")
    public AppResponse updateRole(@RequestBody @Valid RoleUpdateDTO dto, @RequestHeader(value = Const.AUTH) String token) {
        dto.setToken(token);
        sysRoleService.updateRole(dto);
        return AppResponse.success();
    }

    @DeleteMapping("/delete/{roleId}")
    @ApiOperation("删除角色")
    public AppResponse delete(@PathVariable(value = "roleId") Long roleId) {
        sysRoleService.delete(roleId);
        return AppResponse.success();
    }

    @PostMapping("/save/menus")
    @ApiOperation("保存角色与菜单的关系")
    public AppResponse saveRoleMenus(@RequestBody @Valid RoleMenuAddDTO dto) {
        sysRoleMenuService.saveRoleMenu(dto);
        return AppResponse.success();
    }

    @PostMapping("/menu/{roleId}")
    @ApiOperation("根据角色ID查询对应菜单ID")
    public AppResponse<List<Long>> getRoleMenus(@PathVariable(value = "roleId") Long roleId) {
        List<SysRoleMenu> roleMenuList = sysRoleMenuService.list(new QueryWrapper<SysRoleMenu>().eq("role_id", roleId));
        List<Long> collect = roleMenuList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        return AppResponse.success().setRecords(collect);
    }
}
